package com.artisan.requestbodycache.config;

import com.artisan.requestbodycache.filter.CacheRequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * Request Body 缓存自动配置类
 * 核心：注册最高优先级过滤器，确保在所有其他过滤器之前包装 Request
 */
@Configuration
public class CacheRequestAutoConfig {
    
    /**
     * 注册最高优先级过滤器，保证最先包装 Request
     * 优先级设置为 HIGHEST_PRECEDENCE，确保在：
     * - 日志过滤器
     * - 权限过滤器
     * - 业务过滤器
     * 之前执行
     */
    @Bean
    public FilterRegistrationBean<CacheRequestFilter> cacheRequestFilter() {
        FilterRegistrationBean<CacheRequestFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new CacheRequestFilter());
        bean.addUrlPatterns("/*");
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE); // 关键：最高优先级
        bean.setName("cacheRequestFilter");
        return bean;
    }
}

